package com.itboye.ihomebank.home;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;

import com.baidu.location.Poi;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.fangyuan.ChuZuFangShiAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.me.ActivityFaBuHouseTwo;
import com.itboye.ihomebank.util.ByAlert;

public class ActivityEditFangZuXinXi extends BaseOtherActivity {
	ImageView close_icon;
	TextView add_lift_tv, save, txt_fenqishu;
	ToggleButton toggle_button;
	RelativeLayout xuanzefenqi;
	LinearLayout qishu_container;
	private RelativeLayout layout;
	private PopupWindow mPopWin;
	protected int totalFenQi;
	ArrayList<EditText> arrayListEdit = new ArrayList<>();
	boolean isExpand = false;
	int qishu;
	private ArrayAdapter adapter;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.save:
			boolean isEmpty = false;

			StringBuffer stringBuffer = new StringBuffer();
			for (int i = 0; i < arrayListEdit.size(); i++) {
				String edit = arrayListEdit.get(i).getText().toString();
				if (edit.equals("")) {
					isEmpty = true;
					break;
				} else {
					if (i == 0) {
						stringBuffer.append(Double.parseDouble(edit
								+ Double.parseDouble(getIntent()
										.getStringExtra("yajin")))
								+ "、");
					} else {
						stringBuffer.append(edit + "、");
					}
				}
			}
			if (isEmpty) {
				ByAlert.alert("请将租金信息输入完整");
				return;
			}
			intent = new Intent();
			intent.putExtra("zujinxinxi",
					stringBuffer.length() > 0 ? stringBuffer.toString()
							.substring(0, stringBuffer.length() - 1) : "");
			setResult(106, intent);
			finish();
			break;
		case R.id.xuanzefenqi:
			chuzuPopWindow(v);
			break;
		case R.id.toggle_button:

			if (isExpand == false) {
				if (totalFenQi == 0) {
					ByAlert.alert("请先选择分期");
					return;
				}
				fenqi(totalFenQi);
			} else {
				qishu_container.removeAllViews();
				arrayListEdit.clear();
			}

			isExpand = !isExpand;
			break;
		default:
			break;
		}
	}

	private void fenqi(int totalFenQi) {
		// TODO Auto-generated method stub
		qishu_container.removeAllViews();
		for (int i = 0; i < totalFenQi; i++) {
			View view = LayoutInflater.from(this).inflate(
					R.layout.item_hetong, null);
			LayoutParams layoutParams = new LayoutParams(
					LayoutParams.MATCH_PARENT, 70);
			layoutParams.setMargins(0, 5, 0, 5);
			TextView textView = (TextView) view.findViewById(R.id.txt);
			textView.setText("第" + (i + 1) + "期");

			EditText edt = (EditText) view.findViewById(R.id.edit);
			edt.setInputType(InputType.TYPE_CLASS_NUMBER);
			edt.setTextSize(16);
			arrayListEdit.add(edt);
			edt.setHint("输入第" + (i + 1) + "租金");
			qishu_container.addView(view, layoutParams);
		}
	}

	/**
	 * 出租方式
	 */
	private void chuzuPopWindow(View v) {
		layout = (RelativeLayout) LayoutInflater.from(
				ActivityEditFangZuXinXi.this).inflate(R.layout.popup_fangyuan,
				null);
		ArrayList<Integer> data = new ArrayList<Integer>();
		for (int i = 0; i < qishu; i++) {
			data.add(i + 1);
		}
		ListView listView = (ListView) layout
				.findViewById(R.id.fangyuan_listView);
		android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		listView.setLayoutParams(params);
		adapter=new ArrayAdapter(this,
				R.layout.text, data);
		listView.setAdapter(adapter);
		mPopWin = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT, true);
		mPopWin.setBackgroundDrawable(new BitmapDrawable());
		mPopWin.showAsDropDown(v, 0, 0);
		mPopWin.update();

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ((position + 1) == totalFenQi) {
					qishu_container.removeAllViews();
				}
				txt_fenqishu.setText((position + 1) + "");
				totalFenQi = position + 1;
				if (totalFenQi > 0) {
					toggle_button.setEnabled(true);
				}
				if(isExpand){
					fenqi(totalFenQi);
				}

				mPopWin.dismiss();
			}
		});

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_hetong_fangzuxinxi;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		save.setVisibility(View.VISIBLE);
		save.setTextSize(16);
		add_lift_tv.setText("填写房租信息");
		qishu = getIntent().getIntExtra("qishu", 0);
		if (qishu == 0) {
			toggle_button.setEnabled(false);
		} else {
			toggle_button.setEnabled(true);
		}
		txt_fenqishu.setText(qishu + "");
		totalFenQi = qishu;

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

}
